package com.kangyi.admin.service.Impl;

import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kangyi.admin.mapper.AdminUserMapper;
import com.kangyi.admin.mapper.HealthDataMapper;
import com.kangyi.admin.service.HealthDataService;
import com.kangyi.constant.MessageConstant;
import com.kangyi.context.BaseContext;
import com.kangyi.dto.AddHealthDataDTO;
import com.kangyi.dto.EditHealthDataDTO;
import com.kangyi.dto.HealthDataPageQueryDTO;
import com.kangyi.entity.FourHigh;
import com.kangyi.exception.DataNotExistException;
import com.kangyi.exception.PermissionsException;
import com.kangyi.result.HealthDataPageResult;
import com.kangyi.vo.AdminUserVO;
import com.kangyi.vo.GuestVO;
import com.kangyi.vo.HealthDataDetailVO;
import com.kangyi.vo.HealthDataPageQueryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Blank
 * @version 1.0
 */
@Service
@Slf4j
public class HealthDataServiceImpl implements HealthDataService {

    @Autowired
    private HealthDataMapper healthDataMapper;

    @Autowired
    private AdminUserMapper adminUserMapper;

    // 调用后检查当前线程id是否有足够的权限进行后续操作
    public void validateUserPermissions() {
        Integer currentId = BaseContext.getCurrentId();
        AdminUserVO adminUserVO = adminUserMapper.selectById(currentId);
        // 判断权限是否既不是1也不是2，如果是则抛出异常
        if (!adminUserVO.getPermissions().equals(1) && !adminUserVO.getPermissions().equals(2) && !adminUserVO.getPermissions().equals(3)) {
            throw new PermissionsException(MessageConstant.INSUFFICIENT_PRIVILEGE);
        }
    }

    @Override
    public HealthDataPageResult pageQuery(HealthDataPageQueryDTO healthDataPageQueryDTO) {
        validateUserPermissions();
        PageHelper.startPage(healthDataPageQueryDTO.getPage(),healthDataPageQueryDTO.getPageSize());
        Page<HealthDataPageQueryVO> page = healthDataMapper.pageQuery(healthDataPageQueryDTO);
        long total = page.getTotal();
        List<HealthDataPageQueryVO> result = page.getResult();

        // 初始化不同状态的数量
        int normal = 0;
        int onTheHighSide = 0;
        int high = 0;
        int extremelyHigh = 0;

        // 遍历结果列表，统计不同状态的数量
        for (HealthDataPageQueryVO vo : result) {
            Integer status = vo.getStatus();
            if (status != null) {
                if (status == 1) {
                    normal++;
                } else if (status == 2) {
                    onTheHighSide++;
                } else if (status == 3) {
                    high++;
                } else if (status == 4) {
                    extremelyHigh++;
                }
            }
        }
        return new HealthDataPageResult(total,result,normal,onTheHighSide,high,extremelyHigh);
    }

    @Override
    public void deleteHealthData(List<Integer> ids) {
        validateUserPermissions();
        healthDataMapper.deleteHealthData(ids);
    }

    @Override
    public void addHealthData(AddHealthDataDTO addHealthDataDTO) {
        validateUserPermissions();
        FourHigh fourHigh = FourHigh.builder()
                .guestId(addHealthDataDTO.getGuestId())
                .bloodPressureSystolic(addHealthDataDTO.getBloodPressureSystolic())
                .bloodPressureDiastolic(addHealthDataDTO.getBloodPressureDiastolic())
                .bloodSugar(addHealthDataDTO.getBloodSugar())
                .bloodLipids(addHealthDataDTO.getBloodLipids())
                .uricAcid(addHealthDataDTO.getUricAcid())
                .build();

        healthDataMapper.addHealthData(fourHigh);
    }

    @Override
    public List<GuestVO> getByGuestId(Integer nursingHomeId) {
        validateUserPermissions();
        List<GuestVO> guestVOList = healthDataMapper.getByGuestId(nursingHomeId);
        if (CollUtil.isEmpty(guestVOList)) {
            throw new DataNotExistException(MessageConstant.DATA_NOT_EXIST);
        }
        return guestVOList;
    }

    @Override
    public HealthDataDetailVO detailFourHigh(Integer fourHighId) {
        validateUserPermissions();
        HealthDataDetailVO healthDataDetailVO = healthDataMapper.detailFourHigh(fourHighId);
        if (healthDataDetailVO == null){
            throw new DataNotExistException(MessageConstant.DATA_NOT_EXIST);
        }
        return healthDataDetailVO;
    }

    @Override
    public void editHealthData(EditHealthDataDTO editHealthDataDTO) {
        validateUserPermissions();
        FourHigh build = FourHigh.builder()
                .id(editHealthDataDTO.getId())
                .bloodPressureSystolic(editHealthDataDTO.getBloodPressureSystolic())
                .bloodPressureDiastolic(editHealthDataDTO.getBloodPressureDiastolic())
                .bloodSugar(editHealthDataDTO.getBloodSugar())
                .bloodLipids(editHealthDataDTO.getBloodLipids())
                .uricAcid(editHealthDataDTO.getUricAcid())
                .build();
        healthDataMapper.editHealthData(build);
    }
}
